package com.atguigu.crowd.mvc.interceptors;

import com.atguigu.crowd.constant.CrowdContant;
import com.atguigu.crowd.entity.Admin;
import com.atguigu.crowd.exception.AccessForbiddenException;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor extends HandlerInterceptorAdapter {



    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取session对象
        HttpSession session = request.getSession();

        Admin admin = (Admin) session.getAttribute(CrowdContant.ATTR_NAME_ADMIN);

        //判断是否登录
        if (admin==null){
            throw   new AccessForbiddenException();
        }

        //登录了就放行
        return true;
    }
}
